package com.example.myggnerate.unit;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.example.myggnerate.config.GenerateConfig;
import com.example.myggnerate.unit.MyGenerate;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

@Component
public class MyTemplateEngine extends VelocityTemplateEngine {
    public int a = 0;
    @Resource
    private GenerateConfig generateConfig;

    @Override
    public Map<String, Object> getObjectMap(ConfigBuilder config, TableInfo tableInfo) {

        // 获取object map
        Map<String, Object> objectMap = super.getObjectMap(config, tableInfo);
        Set<String> importPackages = tableInfo.getImportPackages();
        importPackages.remove(generateConfig.getEntitySuperClassName());
        // 获取实体类名字
        String entityLowerName = tableInfo.getEntityName().substring(0,1).toLowerCase()+tableInfo.getEntityName().substring(1);
        objectMap.put("entityLowerName", entityLowerName);
        return objectMap;
    }

    @Override
    protected void outputCustomFile(List<CustomFile> customFiles, TableInfo tableInfo, Map<String, Object> objectMap) {
        String entityName = tableInfo.getEntityName();
        String parentPath = this.getPathInfo(OutputFile.parent);
        Map<String, String> packageMap = new HashMap<>();
        packageMap.putAll((Map<String, String>) objectMap.get("package"));
        String parentPackage = packageMap.get("Parent");
        for (CustomFile file : customFiles) {
            if (file.getFileName().contains("java")) {
                String replace = file.getFileName().replace(".java", "");
                String thisPackage = parentPackage +"."+ file.getPackageName();
                packageMap.put(replace, thisPackage);
                objectMap.put("package", packageMap);
            }
            String filePath = StringUtils.isNotBlank(file.getFilePath()) ? file.getFilePath() : parentPath;
            if (StringUtils.isNotBlank(file.getPackageName())) {
                filePath = filePath + File.separator + file.getPackageName().replaceAll("\\.", "\\" + File.separator);
            }
            Function<TableInfo, String> formatNameFunction = file.getFormatNameFunction();
            String fileName = filePath + File.separator + (null != formatNameFunction ? (String) formatNameFunction.apply(tableInfo) : entityName) + file.getFileName();


            if (file instanceof MyGenerate.MyCustomFile) {
                MyGenerate.MyCustomFile myCustomFile = (MyGenerate.MyCustomFile) file;
                if (myCustomFile.getTotalRunTimes() != -1) {//只执行一次
                    if (myCustomFile.getCurrentRunTimes() > myCustomFile.getTotalRunTimes()) {
                        continue;
                    }
                    myCustomFile.setCurrentRunTimes(myCustomFile.getCurrentRunTimes() + 1);
                    fileName = filePath + File.separator + file.getFileName();
                }
            }
            this.outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride());
        }
    }

}